TARGET_NAME = Speaking_thermometer
DEVICE = attiny85
BOOTLOADER_ADDRESS = 0
F_CPU = 16000000UL
FUSEH = 0xdf
FUSEL = 0xe1
AVRDUDE = avrdude -c stk500v2 -P avrdoper -p $(DEVICE)

TOOLPATH = C:/WinAVR-20100110

TARGET_ELF = $(TARGET_NAME).elf
TARGET = $(TARGET_NAME).hex

HFUSES_FILE = $(TARGET_NAME)_HFUSES.txt
LFUSES_FILE = $(TARGET_NAME)_LFUSES.txt

MAKE_PATH = .
SOURCE_PATH = $(MAKE_PATH)/../Source
TARGET_PATH = $(MAKE_PATH)/../Target

include *.mk


ObjWithPath = $(addprefix ${TARGET_PATH}/,$(notdir $(addsuffix .o, $(basename $(source)))))
ObjList = $(notdir $(addsuffix .o, $(basename $(source))))
VPathVar = $(addsuffix :,$(sort $(dir ${source}))) : ${TARGET_PATH}
IPath = $(addprefix -I, $(sort $(dir ${header})))
NameList = $(notdir $($(basename $(source))))

vpath  % ${VPathVar}

CC = avr-gcc
CC_DEFINE = -mmcu=$(DEVICE) -DF_CPU=$(F_CPU) -DMODE=0
LDFLAGS += -Wl,--section-start=.text=$(BOOTLOADER_ADDRESS),-Map=$(TARGET_PATH)/$(TARGET_NAME).map

CFLAGS = $(CC_DEFINE)
CFLAGS += -Wall -gdwarf-2 -std=gnu99 -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums

ASMFLAGS = $(CC_DEFINE)
ASMFLAGS += $(CFLAGS)
ASMFLAGS += -x assembler-with-cpp -Wa,-gdwarf2

COMPILE = $(CC) $(CFLAGS)
COMPILE_ASM = $(CC) $(ASMFLAGS)

HEX_FLASH_FLAGS = -R .eeprom -R .fuse -R .lock -R .signature

HEX_EEPROM_FLAGS = -j .eeprom
HEX_EEPROM_FLAGS += --set-section-flags=.eeprom="alloc,load"
HEX_EEPROM_FLAGS += --change-section-lma .eeprom=0 --no-change-warnings

.PHONY: build
build: $(TARGET) $(TARGET_NAME).lss size

$(TARGET_ELF): $(ObjList)
	@echo Linkuju...
	@$(COMPILE) -o $(TARGET_PATH)/$(TARGET_ELF) $(ObjWithPath) $(LDFLAGS)
	@echo ...OK

%.o: %.c
	@echo Kompiluju $(notdir $<)...
	@$(COMPILE) -c $< -o ${TARGET_PATH}/$@
	@echo ...OK

%.o: %.S
	@echo Kompiluju $(notdir $<)...
	@$(COMPILE_ASM) -x assembler-with-cpp -c $< -o ${TARGET_PATH}/$@

%.hex: $(TARGET_ELF)
	@avr-objcopy -O ihex $(HEX_FLASH_FLAGS)  ${TARGET_PATH}/$< ${TARGET_PATH}/$@

%.eep: $(TARGET_ELF)
	@-avr-objcopy $(HEX_EEPROM_FLAGS) -O ihex $< $@ || exit 0

%.lss: $(TARGET_ELF)
	@avr-objdump -h -S ${TARGET_PATH}/$< > $(TARGET_PATH)/$@

size: ${TARGET_ELF}
	@echo
	@avr-size -C --mcu=${DEVICE} ${TARGET_PATH}/${TARGET_ELF}

.PHONY: clean
clean:
	@echo Mazu *.o, .hex*, *.map soubory...
	@rm -f $(ObjWithPath) 
	@rm -f ${TARGET_PATH}/*.hex
	@rm -f ${TARGET_PATH}/*.map
	@rm -f ${TARGET_PATH}/*.disasm
	@rm -f ${TARGET_PATH}/*.elf
	@rm -f ${TARGET_PATH}/*.lss
	@echo ...OK

.PHONY: dep
dep:
	@echo Hledam zavislosti...
	@avr-gcc ${IPath} -MM -E ${source} $(CC_DEFINE) > depedencies.mk
	@echo ...OK

.PHONY: list
list:
	@echo Hledam zdrojove soubory...
	@echo source=\\\ > tmp_source.mk
	@find $(SOURCE_PATH) -name "*.c" -printf "\t%p\134\n" >> tmp_source.mk
	@find $(SOURCE_PATH) -name "*.S" -printf "\t%p\134\n" >> tmp_source.mk
	@echo >> tmp_source.mk
	@echo header=\\\ > tmp_header.mk
	@find $(SOURCE_PATH) ${TOOLPATH} -name "*.h" -printf "\t%p\134\n" >> tmp_header.mk
	@echo >> tmp_header.mk
	@echo ...OK

.PHONY: disasm
disasm:
	avr-objdump -d $(TARGET) > $(TARGET_NAME).disasm

.PHONY: flash
flash:
	$(AVRDUDE) -U flash:w:$(TARGET_PATH)/$(TARGET):i

.PHONY: write_fuses
write_fuses:
	$(AVRDUDE) -U hfuse:w:$(FUSEH):m -U lfuse:w:$(FUSEL):m

.PHONY: reset
reset:
	$(AVRDUDE) -E reset

.PHONY: read_fuses
read_fuses:
	$(AVRDUDE) -U hfuse:r:$(TARGET_PATH)/$(HFUSES_FILE):r -U lfuse:r:$(TARGET_PATH)/$(LFUSES_FILE):r

.PHONY: all
all:
	@make -s clean
	@make -s list
	@make -s dep
	@make -s build

